package com.yuncheng.ccsystem.announce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.ccsystem.announce.entity.CcAnnouncement;
import com.yuncheng.ccsystem.announce.mapper.CcAnnouncementSendMapper;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.constant.CommonConstant;
import com.yuncheng.ccsystem.announce.entity.CcAnnouncementSend;
import com.yuncheng.ccsystem.announce.mapper.CcAnnouncementMapper;
import com.yuncheng.ccsystem.announce.service.ICcAnnouncementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@Service
public class CcAnnouncementServiceImpl extends BaseServiceImpl<CcAnnouncementMapper, CcAnnouncement> implements ICcAnnouncementService {

	@Resource
	private CcAnnouncementMapper ccAnnouncementMapper;

	@Resource
	private CcAnnouncementSendMapper ccAnnouncementSendMapper;

	@Transactional
	@Override
	public void saveAnnouncement(CcAnnouncement ccAnnouncement) {
		if(ccAnnouncement.getMsgType().equals(CommonConstant.MSG_TYPE_ALL)) {
			ccAnnouncementMapper.insert(ccAnnouncement);
		}else {
			// 1.插入通告表记录
			ccAnnouncementMapper.insert(ccAnnouncement);
			// 2.插入用户通告阅读标记表记录
			String userId = ccAnnouncement.getUserIds();
			String[] userIds = userId.substring(0, (userId.length()-1)).split(",");
			String anntId = ccAnnouncement.getId();
			Date refDate = new Date();
			for(int i=0;i<userIds.length;i++) {
				CcAnnouncementSend announcementSend = new CcAnnouncementSend();
				announcementSend.setAnntId(anntId);
				announcementSend.setUserId(userIds[i]);
				announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
				announcementSend.setReadTime(refDate);
				ccAnnouncementSendMapper.insert(announcementSend);
			}
		}
	}

	/**
	 * @功能：编辑消息信息
	 */
	@Transactional
	@Override
	public boolean upDateAnnouncement(CcAnnouncement ccAnnouncement) {
		// 1.更新系统信息表数据
		ccAnnouncementMapper.updateById(ccAnnouncement);
		String userId = ccAnnouncement.getUserIds();
		if(!StringUtils.isEmpty(userId)&& ccAnnouncement.getMsgType().equals(CommonConstant.MSG_TYPE_UESR)) {
			// 2.补充新的通知用户数据
			String[] userIds = userId.substring(0, (userId.length()-1)).split(",");
			String anntId = ccAnnouncement.getId();
			Date refDate = new Date();
			for(int i=0;i<userIds.length;i++) {
				LambdaQueryWrapper<CcAnnouncementSend> queryWrapper = new LambdaQueryWrapper<CcAnnouncementSend>();
				queryWrapper.eq(CcAnnouncementSend::getAnntId, anntId);
				queryWrapper.eq(CcAnnouncementSend::getUserId, userIds[i]);
				List<CcAnnouncementSend> announcementSends= ccAnnouncementSendMapper.selectList(queryWrapper);
				if(announcementSends.size()<=0) {
					CcAnnouncementSend announcementSend = new CcAnnouncementSend();
					announcementSend.setAnntId(anntId);
					announcementSend.setUserId(userIds[i]);
					announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
					announcementSend.setReadTime(refDate);
					ccAnnouncementSendMapper.insert(announcementSend);
				}
			}
			// 3. 删除多余通知用户数据
			Collection<String> delUserIds = Arrays.asList(userIds);
			LambdaQueryWrapper<CcAnnouncementSend> queryWrapper = new LambdaQueryWrapper<CcAnnouncementSend>();
			queryWrapper.notIn(CcAnnouncementSend::getUserId, delUserIds);
			queryWrapper.eq(CcAnnouncementSend::getAnntId, anntId);
			ccAnnouncementSendMapper.delete(queryWrapper);
		}
		return true;
	}

	// @功能：流程执行完成保存消息通知
	@Override
	public void saveSysAnnouncement(String title, String msgContent) {
		CcAnnouncement announcement = new CcAnnouncement();
		announcement.setTitile(title);
		announcement.setMsgContent(msgContent);
		announcement.setSender("YCH BOOT");
		announcement.setPriority(CommonConstant.PRIORITY_L);
		announcement.setMsgType(CommonConstant.MSG_TYPE_ALL);
		announcement.setSendStatus(CommonConstant.HAS_SEND);
		announcement.setSendTime(new Date());
//		announcement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
		ccAnnouncementMapper.insert(announcement);
	}

	@Override
	public Page<CcAnnouncement> querySysCementPageByUserId(Page<CcAnnouncement> page, String userId, String msgCategory) {
		 return page.setRecords(ccAnnouncementMapper.querySysCementListByUserId(page, userId, msgCategory));
	}

}
